package org.hhchat.leetcode.string;

/**
 * https://leetcode.com/problems/count-and-say/description/
 */

public class code38_easy {
    static class Solution {
        public String countAndSay(int n) {
            String string="";
            for(int i=1;i<=n;i++) {
                if(i==1){
                    string = "1";
                }
                else{
                    string = generateNext(string);
                }
            }
            return string;
        }


        public String generateNext(String str){
            char curChar = str.charAt(0);
            int count=0;
            boolean finished;
            StringBuilder stringBuilder = new StringBuilder();
            for(int i=0;i<str.length();i++) {
                if (curChar == str.charAt(i)) {
                    count++;
                }else{
                    stringBuilder.append(count);
                    stringBuilder.append(curChar);
                    curChar=str.charAt(i);
                    count=1;
                }
            }
            if (count != 0) {
                stringBuilder.append(count);
                stringBuilder.append(curChar);
            }
            return stringBuilder.toString();
        }
    }

    public static void main(String[] args){
        Solution solution = new Solution();
        System.out.println(solution.countAndSay(6));
    }
}
